/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TGasCard;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.GasCardPayRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.GasCardPayResponse;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.GasCardPayResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.widget.WheelPicker;

public class GasCardPayActivity extends BaseActivity implements OnClickListener, OnResponseListener {
	private Context mContext;
	TGasCard card;
	TextView code;
	TextView price;
	TextView name;
	TextView phone;
	TextView address;
	TextView idcard;

	private GasCardPayRequestBuilder mReq = new GasCardPayRequestBuilder();
	private GasCardPayResponseHandler mHandler = new GasCardPayResponseHandler();

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gas_card_pay);
		mContext = this;
		card = (TGasCard) getIntent().getSerializableExtra("data");
		if (card == null) {
			Toast.makeText(mContext, "该卡不存在", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		code = (TextView) findViewById(R.id.gas_pay_code);
		price = (TextView) findViewById(R.id.gas_pay_price);
		name = (TextView) findViewById(R.id.gas_pay_name);
		phone = (TextView) findViewById(R.id.gas_pay_phone);
		address = (TextView) findViewById(R.id.gas_pay_address);
		idcard = (TextView) findViewById(R.id.gas_pay_idcard);
	}

	public void reg() {
		price.setOnClickListener(this);
		findViewById(R.id.gas_pay_ok).setOnClickListener(this);
		mHandler.setOnResponseListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		code.setText(card.getCode());
		name.setText("*" + card.getName().substring(1));
		phone.setText("*" + card.getPhone().substring((int) (card.getPhone().length() * 0.5)));
		address.setText("*" + card.getAddress().substring((int) (card.getAddress().length() * 0.5)));
		idcard.setText("*" + card.getIdcard().substring((int) (card.getIdcard().length() * 0.5)));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case R.id.gas_pay_price:
				price.setText(data.getStringExtra("resultdata"));
				break;
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.gas_pay_price:
			Intent bodyIntent = new Intent(mContext, WheelPicker.class);
			String[] array = new String[App.CHARGE_VALUE_MAP.get("charge").size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = App.CHARGE_VALUE_MAP.get("charge").get(i);
			}
			bodyIntent.putExtra("array", array);
			startActivityForResult(bodyIntent, R.id.gas_pay_price);
			break;
		case R.id.gas_pay_ok:
			mReq.setCode(card.getCode()).setPrice(price.getText().toString()).postJsonRequest(mContext, mHandler);
			break;
		default:
			break;
		}

	}

	@Override
	public void onResponse(IResponseHandler classz) {
		IResponse res = classz.getResponse();
		if(res instanceof GasCardPayResponse){
			TProduct product = new TProduct();
			product.body = "加油卡";
			product.price = Double.valueOf(price.getText().toString());
			product.subject = "车拉丁";
			product.table = "t_gas_card_pay";
			product.tableId = ((GasCardPayResponse) res).pay.getId();
			product.userid = new UserDao().getUser().id;
			Intent i = new Intent(mContext, PayActivity.class);
			i.putExtra("data", product);
			startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
}
